﻿using System;
using System.Collections.Generic;
using System.ServiceModel.Syndication;
using System.Web;
using RazakiBLL;
using RazakiBLL.Entities;
using RazakiBLL.Managers;

namespace RazakiSite.PagesNews
{
    /// <summary>
    /// Summary description for RSSNews
    /// </summary>
    public class RSSNews : IHttpHandler
    {
        /// <summary>
        /// tagid is last child.
        /// </summary>
        private short TagId
        {
            get { return UtilitiesUI.GetQueryString<short>("tagid", false); }
        }

        public void ProcessRequest(HttpContext context)
        {
            FeedHelper feedHelper = new FeedHelper();
            UowRazaki uowRazaki = new UowRazaki();
            string queueDesc, feedUrl, feedImageUrl;
            short parentId = uowRazaki.NewTagRepository.GetFirstParentByLastChildId(TagId).Id;
            List<SyndicationItem> items;
            SyndicationItem item;
            byte[] rss;
            List<New> news;
            string siteUrl = UtilitiesUI.SiteAddress(context.Request);


            queueDesc = uowRazaki.NewTagRepository.GetDescQueue(TagId);
            feedUrl = string.Format("{0}PagesNews/NewsList.aspx{1}",
                                    siteUrl,
                                    UtilitiesUI.SetQueryString(
                                        new Dictionary<string, string>()
                                            {
                                                {"parentid", parentId.ToString()}
                                            }, false));
            feedImageUrl = string.Format("{0}Images/logo.png", siteUrl);

            news = uowRazaki.NewRepository.GetAllByTagId(TagId);
            items = new List<SyndicationItem>();

            foreach (var @new in news)
            {
                string itemId = "ItemID";
                string newUrl =
                    string.Format("{0}PagesNews/NewsView.aspx{1}", siteUrl,
                                  UtilitiesUI.SetQueryString(new Dictionary<string, string>()
                                                                 {
                                                                     {"newid", @new.Id.ToString()}
                                                                 }, false));
                //todo: اگر در متن از تگ های 
                //html
                //استفاده شده است. حذف شوند.
                string content = UtilitiesBLL.ShortText(@new.Text, 60);

                item = new SyndicationItem(@new.Title, new TextSyndicationContent(content), new Uri(newUrl), itemId,
                                           @new.UpdateDate);

                items.Add(item);
            }

            rss = feedHelper.GetRSS(queueDesc, queueDesc, feedUrl, feedImageUrl, items);

            context.Response.ContentType = "text/xml";
            context.Response.Charset = "UTF-8";
            context.Response.BinaryWrite(rss);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}